package com.bosssoft.cloud.wechat.common.controller;

import com.bosssoft.cloud.config.domain.InformationPublish;
import com.bosssoft.cloud.config.domain.InformationPublishDetailVO;
import com.bosssoft.cloud.config.domain.InformationPublishFile;
import com.bosssoft.cloud.config.domain.PageVO;
import com.bosssoft.cloud.config.service.InformationPublishService;
import com.bosssoft.cloud.wechat.infrastructure.bean.response.R;
import com.bosssoft.cloud.wechat.infrastructure.enums.ErrEnum;
import com.bosssoft.cloud.wechat.infrastructure.enums.ResultEnum;
import com.bosssoft.cloud.wechat.infrastructure.util.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * 政策法规
 *
 * @author wcy create on 2021/6/29 17:21 下午
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/information")
@Api(value = "InformationPublishController", tags = "政策法规查询")
public class InformationPublishController {


    @Resource
    private InformationPublishService informationPublishService;

    @GetMapping("select_page")
    @ApiOperation("分页获取政策法规数据")
    public R<?> selectPage(
            @RequestParam(name = "pageNo") Integer pageNo, @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "infoType") Integer infoType) {
        PageVO<InformationPublish> informationPublishPageVO = informationPublishService.selectPage(pageNo, pageSize, infoType);
        Assert.notNull(informationPublishPageVO, ErrEnum.C0114);
        return new R<>(ResultEnum.SUCCESS, informationPublishPageVO);
    }

    @GetMapping("detail")
    @ApiOperation("政策法规详情")
    public R<?> detail(
            @RequestParam(name = "informationId") String informationId) {
        InformationPublishDetailVO detail = informationPublishService.detail(informationId);
        Assert.notNull(detail, ErrEnum.C0114);
        return new R<>(ResultEnum.SUCCESS, detail);
    }

    @GetMapping("download_file")
    @ApiOperation("附件下载")
    public void downloadFile(
            @RequestParam(name = "fileId") String fileId, HttpServletRequest request, HttpServletResponse response) {
        InformationPublishFile informationPublishFile = informationPublishService.fileDownload(fileId);
        Assert.notNull(informationPublishFile, ErrEnum.C0115, "fileId:" + fileId + "not found.");
        log.info("fileDownload, file name is {}", informationPublishFile.getFileName());
        OutputStream os = null;
        try {
            String userAgent = request.getHeader("User-Agent");
            String formFileName = informationPublishFile.getFileName();
            os = response.getOutputStream();
            // 针对IE或者以IE为内核的浏览器：
            if (userAgent.contains("MSIE") || userAgent.contains("Trident") || userAgent.contains("Edge")) {
                formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");
            } else {
                // 非IE浏览器的处理：
                formFileName = new String(formFileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            }
            response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", formFileName));
            response.setHeader("Connection", "close");
            response.setHeader("Content-Type", "application/octet-stream");
            //获得Oracle数据库blob字段值
            byte[] buffer = informationPublishFile.getFileContent();
            //下载
            os.write(buffer);
            os.flush();
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
